package com.zmx.job.admin.core.conf;

import com.zmx.job.admin.core.scheduler.MallJobScheduler;
import com.zmx.job.admin.core.thread.JobSendHelper;
import com.zmx.job.admin.repository.MallJobClientRepository;
import com.zmx.job.admin.repository.MallJobInfoRepository;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * @Description: 服务端主配置，启动类
 * @ClassName: MallAdminConfig
 * @Author zhaomxr
 * @Date 2021-12-20 16:13
 */
@Component
@Getter
@Setter
public class MallAdminConfig implements InitializingBean, DisposableBean {

    @Value("${auth.username}")
    private String username;

    @Value("${auth.password}")
    private String password;

    @Value("${auth.secret}")
    private String secret;

    @Value("${job.max.meanwhile:100}")
    private int maxMeanwhile;

    @Value("${job.request.url.prefix}")
    private String requestUrlPrefix;

    @Value("${job.request.retry:true}")
    private boolean requestRetry;

    @Value("${job.request.retry.count:1}")
    private int requestRetryCount;

    // 容器bean供给自旋调度任务线程使用----------------------------------------------------------

    @Autowired
    private JobSendHelper jobSendHelper;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private MallJobInfoRepository mallJobInfoRepository;
    @Autowired
    private MallJobClientRepository mallJobClientRepository;

    private static MallAdminConfig mallAdminConfig;

    public static MallAdminConfig getMallAdminConfig() {
        return mallAdminConfig;
    }

    public MallJobInfoRepository getMallJobInfoRepository() {
        return mallJobInfoRepository;
    }

    public MallJobClientRepository getMallJobClientRepository(){return mallJobClientRepository;}
    // 容器bean供给自旋调度任务线程使用----------------------------------------------------------

    private MallJobScheduler mallJobScheduler;

    @Override
    public void afterPropertiesSet() throws Exception {
        mallAdminConfig = this;
        mallJobScheduler = new MallJobScheduler();
        mallJobScheduler.init();
    }

    @Override
    public void destroy() throws Exception {
        mallJobScheduler.destory();
    }
}
